﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Cosmetic.Logic;
using Cosmetic.DataEntity;

namespace WebApp.Shopping
{
    public partial class zjNotify : System.Web.UI.Page
    {
        PurchaseManager pm = new PurchaseManager();
        UserManager m_UserManager = new UserManager();
        IntegralManager m_IntegralManager = new IntegralManager();

        protected void Page_Load(object sender, EventArgs e)
        {
            string merId = Request.Params["mer_id"];
            string orderDate = Request.Params["order_date"];
            string orderNo = Request.Params["order_no"];
            string finishTime = Request.Params["finish_time"];
            string orderAmount = Request.Params["order_amount"];
            string status = Request.Params["status"];
            string mobile = Request.Params["mobile"];
            string paymentType = Request.Params["payment_type"];


            string sign = Request.Params["sign"];
            string data = "mer_id=" + merId + ",order_date=" + orderDate + ",order_no=" + orderNo;
            data = data + ",finish_time=" + finishTime + ",order_amount=" + orderAmount;
            data = data + ",status=" + status + ",mobile=" + mobile;
            data = data + ",payment_type=" + paymentType;

            string key = "eb0f94856f956d35c58d";
            skypay.signature.Signature md5sign = new skypay.signature.Signature();
            string md5 = md5sign.GetMD5Sign(data, key);
            if (md5 == sign)
            {
                try
                {
                    if (!pm.ValidateOrder(orderNo))
                    {
                        UpdateOrder(orderNo);
                    }
                    Label1.Text = @"您的交易已完成，我们会尽快发送你要的货物。<br />请留意您的电话，谢谢！";
                }
                catch (Exception ex)
                {
                    this.Label1.Text = ex.Message;
                }
            }
            else
            {
                Label1.Text = "error <br/>" + data;
            }
        }

        private void UpdateOrder(string trade_no)
        {
            string CustomerCode = "FM9999";
            string CustomerName = "WebCustomer";
            if (!pm.ValidateOrder(trade_no))
            {
                try
                {
                    Order theOrder = pm.GetOrderByTradeNo(trade_no);
                    Cosmetic.DataEntity.User user = m_UserManager.GetUserById(theOrder.UserId);
                    user.Integral -= theOrder.UseIntegral.Value;
                    m_UserManager.Update();
                    Session["SignInUser"] = user;
                    theOrder.Valid = true;
                    pm.ValidOrder(theOrder);
                    pm.CreateOrderInHZ(trade_no, CustomerCode, "我付了", "Admin");
                    //ali
                    pm.CreateOrderDetailInHZ(trade_no);
                    m_IntegralManager.UseCashCard(theOrder);
                    m_IntegralManager.UseDiscountCard(theOrder);
                    //Response.Write("success");     //返回给支付宝消息，成功，请不要改写这个success
                }
                catch
                {
                    //Response.Write("fail");
                    throw new Exception("Update Website error");
                }
            }
        }
    }
}
